package com.xn.app.muying.activity;

import android.app.Application;
import android.os.Environment;
import android.util.Log;

import com.xn.app.muying.sqlite.DBManager;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import cn.jiguang.share.android.api.JShareInterface;
import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;
import okhttp3.OkHttpClient;

public class MuYingApplication extends Application {
    public static OkHttpClient mOkHttpClient;
    private static MuYingApplication sInstance;
    //数据库
    private DBManager db = null;

    public static String ImagePath;
    public static String VideoPath;

    public static MuYingApplication getInstance() {
        return sInstance;
    }

    @Override
    public void onCreate() {
        db = new DBManager(this);
        super.onCreate();
        mOkHttpClient = new OkHttpClient.Builder()
                .readTimeout(60, TimeUnit.SECONDS)//设置读取超时时间
                .writeTimeout(60, TimeUnit.SECONDS)//设置写的超时时间
                .connectTimeout(60, TimeUnit.SECONDS)//设置连接超时时间
                .build();

        com.lzy.okhttputils.OkHttpUtils.init(this);
        sInstance = this;

        //初始化极光推送
        JPushInterface.init(this);
        JPushInterface.setDebugMode(true);
        JPushInterface.setAliasAndTags(this, db.select("tel"), new HashSet<String>(), new TagAliasCallback() {
            @Override
            public void gotResult(int i, String s, Set<String> set) {
                //设置别名和标签
            }
        });
        //初始化分享
        JShareInterface.init(this);

        new Thread(){
            @Override
            public void run() {
                File imageFile =  copyResurces( "jiguang_test_img.png", "test_img.png", 0);
                File videoFile = copyResurces( "jiguang.mp4", "jiguang.mp4", 0);
                if(imageFile != null){
                    ImagePath = imageFile.getAbsolutePath();
                }

                if(videoFile != null){
                    VideoPath = videoFile.getAbsolutePath();
                }

                super.run();
            }
        }.start();
    }
    private  File copyResurces(String src, String dest, int flag){
        File filesDir = null;
        try {
            if(flag == 0) {//copy to sdcard
                filesDir = new File(Environment.getExternalStorageDirectory().getAbsoluteFile() + "/jiguang/" + dest);
                File parentDir = filesDir.getParentFile();
                if(!parentDir.exists()){
                    parentDir.mkdirs();
                }
            }else{//copy to data
                filesDir = new File(getFilesDir(), dest);
            }
            if(!filesDir.exists()) {
                filesDir.createNewFile();
                InputStream open = getAssets().open(src);
                FileOutputStream fileOutputStream = new FileOutputStream(filesDir);
                byte[] buffer = new byte[4 * 1024];
                int len = 0;
                while ((len = open.read(buffer)) != -1) {
                    fileOutputStream.write(buffer, 0, len);
                }
                open.close();
                fileOutputStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
            if(flag == 0){
                filesDir = copyResurces( src,  dest, 1);
            }
        }
        return filesDir;
    }
}
